<?php
namespace Components\Account\User\Achievement;

class Service {
  public function findFirst($parameters = []) {
    $model = Model::findFirst($parameters);
    if(!$model) {
      return ['code' => -1, 'message' => '业绩数据不存在或已删除'];
    }

    return ['code' => 0, 'model'=>$model];
  }

  public function findFirstById($id, $parameters = []) {
    $res = $this -> findFirst(array_merge([
      'conditions' => 'user_id = ' . $id,
    ], $parameters));
    
    if($res['code'] != 0) {
      return $this -> create([
        'user_id' => $id,
        'day_amount_left' => 0,
        'day_amount_right' => 0,
        'month_amount_left' => 0,
        'month_amount_right' => 0
      ]);
    }

    return $res;
  }

  public function create($parameters) {
    $model = new Model($parameters);
    if(!$model -> create()) {
      $messages = $model -> getMessages();
      return ['code' => -1, 'message' => $messages[0] -> getMessage()];
    }

    return ['code'=>0,'model'=>$model];
  }
}
